package com.hjm.admin.moule.view.fragment.impl;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hjm.admin.moule.R;
import com.hjm.admin.moule.model.BookClassifyBean;
import com.hjm.admin.moule.utils.DimenUtils;
import com.hjm.admin.moule.view.activity.impl.BookListActivity;
import com.hjm.admin.moule.view.adapter.ClassifyAdapter;
import com.hjm.admin.moule.view.base.BaseFragment;
import com.hjm.admin.moule.view.fragment.IClassifyBook;
import com.hjm.admin.moule.viewmodel.fragment.VMBookClassify;
import com.hjm.admin.moule.widget.SpacesItemDecoration;
import com.weavey.loading.lib.LoadingLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ClassifyFragment extends BaseFragment implements IClassifyBook {
    @BindView(R.id.rv_classify)
    RecyclerView mRvClassify;
    @BindView(R.id.loadinglayout)
    LoadingLayout loadingLayout;

    String tabName;
    ClassifyAdapter mClassifyAdapter;
    List<BookClassifyBean.ClassifyBean> classifyBeans;
    String gender = "male";
    private VMBookClassify mModel;

    public static ClassifyFragment newInstance(String tabName) {
        Bundle bundle = new Bundle();
        bundle.putString("tabName", tabName);
        ClassifyFragment fragment = new ClassifyFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mModel = new VMBookClassify(mContext, this);
        return setContentView(container, R.layout.fragment_classify, mModel);
    }

    @Override
    protected void initView() {
        classifyBeans = new ArrayList<>();
        tabName = getArguments().getString("tabName");
        mModel.bookClassify();
        mClassifyAdapter = new ClassifyAdapter(classifyBeans);
        mRvClassify.setLayoutManager(new LinearLayoutManager(mContext));
        mRvClassify.addItemDecoration(new SpacesItemDecoration(DimenUtils.dp2px(10),DimenUtils.dp2px(10),0,DimenUtils.dp2px(4)));
        mClassifyAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mRvClassify.setAdapter(mClassifyAdapter);
        mClassifyAdapter.setOnItemClickListener((adapter, view, position) -> {
            Bundle bundle = new Bundle();
            bundle.putString("gender", gender);
            bundle.putString("titleName", classifyBeans.get(position).getName());
            startActivity(BookListActivity.class, bundle);
        });
    }

    @Override
    public void getBookClassify(BookClassifyBean bookClassifyBean) {
        loadingLayout.setStatus(loadingLayout.Success);
        classifyBeans.clear();
        switch (tabName) {
            case "男生版":
                gender = "male";
                classifyBeans.addAll(bookClassifyBean.getMale());
                break;
            case "女生版":
                gender = "female";
                classifyBeans.addAll(bookClassifyBean.getFemale());
                break;
            case "出版":
                classifyBeans.addAll(bookClassifyBean.getPress());
                gender = "press";
                break;
        }
        mClassifyAdapter.notifyDataSetChanged();
    }

    @Override
    public void emptyData() {
        loadingLayout.setStatus(LoadingLayout.Empty);
    }

    @Override
    public void errorData(String error) {
        loadingLayout.setEmptyText(error).setStatus(LoadingLayout.Empty);
    }

    @Override
    public void netWorkError() {
        loadingLayout.setStatus(LoadingLayout.No_Network);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void stopLoading() {

    }
}
